Skip to content

Evaluator: a for loop's splat target collects what each gave, as ruby's does - #310

Merged
284km merged 1 commit into
mainfrom
stage10-eval-for-splat
Nov 26, 2026
Merged

284km merged 1 commit into
mainfrom
stage10-eval-for-splat

Conversation

@284km

@284km 284km commented Nov 26, 2026

Copy link
Copy Markdown
Owner

On the evaluator, for *a in o kept only the first of several yielded values ([1] for yield 1, 2) and gave [nil] for a bare yield. Ruby gives [1, 2] and [], and the VM already did. #300's review found it.

for *h[:z] in o and for *s.x in o were SyntaxErrors on both executions; in ruby they take the same values as for *a.

Cause

The parser writes a for loop as each with a block. A lone splat target was given one block parameter |__for| and the assignment *a = __for. One parameter keeps the first of several values and is nil for none.

Ruby (pm_compile_for_node_index) takes the values as a rest parameter. If there is one value and Array.try_convert makes an Array of it, it uses that Array; then it assigns. Neither |x| nor |*x| alone does both.

The for loop's own target reader (parse_for_xtargets) did not accept *.

Change (m_parse.mere, parse_for)

  • for *a (and a splat on an ivar) takes |*__forv| and applies ruby's rule first (for_rest_args). The calls (length, [], ::Array.try_convert) are made, as ruby's sequence makes them.
  • parse_for_xtargets accepts * in front of a target, as multiple assignment does. A lone splat on a complex target (for *h[:z]) goes the same way as for *a.

Left as on main (no new difference between the executions)

  • Several complex targets keep main's evaluator code. In for s.x, b in [[1, 2]], ruby 4.0 sends the writer to one of the values (2.x = 1). The VM runs ruby's sequence and does the same; the evaluator binds [[1, 2], nil] as on main. Matching ruby's quirk on the evaluator is a separate item.
  • A splat among several complex targets (for h[:k], *c) stays a SyntaxError on both executions, as on main. Ruby raises NoMethodError there, from the same quirk.

Checked

form main, evaluator main, VM here, both ruby
for *a over yield 1, 2 / yield [1] / [nil] [1, 2] / [] [1, 2] / [] [1, 2] / []
for *h[:z], for *s.x SyntaxError SyntaxError as ruby
for s.x, b in [[1, 2], [3, 4]] [[[1, 2], nil], ...] NoMethodError unchanged NoMethodError
for h[:k], *c in o SyntaxError SyntaxError unchanged NoMethodError

Record: loop/2026-11-26-stage10-eval-for-splat.md.

🤖 Generated with Claude Code

…'s does

`for *a in o` was a block of one parameter and `*a = __for`: of several
values yielded it kept the first, and a bare yield gave [nil]. It takes
|*__forv| and ruby's rule (pm_compile_for_node_index): the one value as an
Array when Array.try_convert makes one of it, else the values. And a splat
on a complex target, `for *h[:z] in o`, is read (it was a SyntaxError on
both executions) and goes the same way. Several complex targets keep their
way, and a splat among them is still refused.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 21ac1ec. No Critical / Major / Minor findings. The evaluator's for *a in o now binds through |*__forv| with ruby's rule: one value that Array.try_convert turns into an Array gives that Array, otherwise all the values. for *h[:z] and for *s.x now parse on both engines (they were SyntaxErrors). The other complex multi-targets are left as on main, with the ruby quirk for for s.x, b recorded as a separate item, so no new evaluator/VM difference is introduced. I ran corpus 830 and a probe on the author's build at this head, on the evaluator and MERE_RUBY_VM=1, against ruby 4.0.10: an each yielding 1, 2, [3, 4], nothing, nil and [5]; for *h[:z]; for *s.x; Arrays, [], nil and an Integer; and an object with to_ary. All identical. The description has vm_corpus 962 on the VM / 0 FAIL, run_corpus 962/962 and vm_check 246/246 on the rebased build. git merge-tree against main: clean. No mere-pr-review-watch inline comments. Merging.

@284km
284km merged commit 9442479 into main Nov 26, 2026
5 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant